<?php
namespace Home\Controller;
use Lib;
class MessageController extends Lib\Controller {
	function __construct() {
		parent::__construct ();
	}
	/**
	 * 发送消息
	 * 需要提供接收者ID,消息内容
	 * 消息内容，通过JSON字符串表示信息
	 * 文本{content:"..............................."}
	 * 图片{title:"title",id:"id"}
	 * 链接{title:"title",id:"id"}
	 * 音频{title:"title",id:"id"}
	 * 视频{title:"title",id:"id"}
	 */
	public function sendMessage() {
		
		//进行好友验证，是好友才发生消息，不是好友提示错误信息
		
		$m = D ( 'Message' );
		$d = $m->send ( $this->userInfo['user_id'], $this->param['receive_id'], $this->param['message'] ,$this->param['type']);
		//print_r( $this->userInfo);
		if(empty($d)){
			$this->error(ERROR_SQL_FAILING,'数据库操作失败 |  ERROR INFO : ' . $m->getError() . ' | SQL : ' .$m->getLastSql() . __FILE__ . __LINE__);
		}
		$dd = array('id' => $d);
		$this->show($dd);
	}
	/**
	 * 获取消息
	 * 需要传递发送者ID,开始，结束条目
	 */
	public function getMessage(){
		if(empty($this->param['send_id'])){
			$this->error(ERROR_NO_ARGV,"参数不正确，请检查");
		}
		$m = D ( 'Message' );
		$d = $m->getMessage($this->param['send_id'],$this->userInfo['user_id'],$this->param['start'],$this->param['end']);
		
		//标记消息为已读
		$whe = array(
				'receive_id'	=>	$this->param['send_id'],
				'send_id'		=>	$this->userInfo['user_id'],
				'mode'		=>	0//一定要加上，只修改为0的为1，有利于数据库执行速度
		);
		$m->where($whe)->data(array('mode'=>1))->save();
		
		//错误判断
		if(empty($d)){
			$this->error(ERROR_GENERIC,'没有数据');
		}
		$this->show($d);
	}
	/**
	 * 获取未读消息
	 */
	public function getUnreadMessage(){
		if(empty($this->param['send_id'])){
			$this->error(ERROR_NO_ARGV,"参数不正确，请检查");
		}
		$m = D ( 'Message' );
		$whe = array(
			'receive_id'	=> $this->userInfo['user_id'],
			'send_id'		=> $this->param['send_id'],
			'mode'		=> 0
		);
		$d = $m->where($whe)->select();
		//错误判断
		if(empty($d)){
			$this->error(ERROR_GENERIC,'没有数据');
		}
		$this->show($d);
	}
}

?>